package com.apes.fn.fico.expense.platformFee.process;

import com.apes.fn.fico.expense.platformFee.CommonFeeProcess;
import com.apes.fn.scm.used.model.UsedCar;
import com.apes.fn.scm.used.repository.UsedCarRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 二手车生成平台费用细项单据
 * 同一单据可能满足多种条件，无法走公共方法 CommonFeeProcess 中的 createExpense 生成
 */
@Service
public class UsedCarFeeProcess extends CommonFeeProcess {

    @Autowired
    private UsedCarRepository usedCarRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        UsedCar usedCar = usedCarRepository.findOne(id);
        createExpense(MapUtil.mapper("order", usedCar));
    }

    /**
     * 平台服务费
     *
     * @param map {"order": UsedCar}
     */
    @Override
    public Map<String, Object> getExpenseModel(Map map) {
        UsedCar usedCar = (UsedCar) map.get("order");
        double platformFreeRatio = getPlatformFreeRatio(usedCar.getCompany().getId());
        double expenseAmount = usedCar.getStorePrice() * platformFreeRatio;
        return MapUtil.mapper("order", usedCar.getId(), "orderType", "UsedCar",
                "source", usedCar.getId(), "sourceBusinessType", "UsedCar","date",usedCar.getDateApprove(),
                "summary", "二手车服务费", "incomeTypeId", "A09", "expenseDetailsTypeId", "002052",
                "paymentAmount", usedCar.getStorePrice(), "expenseAmount", expenseAmount,
                "deptId", usedCar.getDept().getId(), "companyId", usedCar.getCompany().getId());
    }

    @Override
    public boolean support(String type) {
        return "UsedCar".equals(type);
    }
}
